package com.paomi.client.activity.reply;

import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.activity.detail.model.DetailTicketBean;
import com.paomi.client.activity.detail.model.TipCommentBean;
import com.paomi.client.activity.reply.model.ReplyContent;
import com.paomi.client.activity.reply.model.ReplyJsonBean;
import com.paomi.client.base.BaseLCEActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Datas;
import com.paomi.client.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pc on 2018/11/9
 * Describe:回复列表页面
 * com.paomi.client.activity.reply
 */
public class ReplyActivity extends BaseLCEActivity<ReplyContent, ReplyPresenter> {

    @BindView(R.id.nested_reply)
    NestedScrollView nested_reply;
    @BindView(R.id.reply_list)
    RecyclerView replyList;
    private String getId;//商品id
    private String pid;//被评论的id
    private int page = 1;//页码数

    @BindView(R.id.user_image)
    ImageView user_image;
    @BindView(R.id.comment_user)
    TextView comment_user;
    @BindView(R.id.comment_tip_lay)
    TextView comment_tip_lay;
    @BindView(R.id.msg_num_iv)
    TextView msg_num_iv;
    @BindView(R.id.praise_num)
    TextView praise_num;
    @BindView(R.id.praise_iv)
    TextView praise_iv;
    @BindView(R.id.comment_content)
    TextView comment_content;
    @BindView(R.id.comment_time)
    TextView comment_time;
    @BindView(R.id.tv_main)
    TextView tv_main;
    @BindView(R.id.textview_title)
    TextView textview_title;

    ReplyContent content;


    private boolean isLike = true;
    private DetailTicketBean.TicketBean ticketBean = null;
    private List<ReplyContent> replyContentList=new ArrayList<>();
    private List<TipCommentBean> listTip;

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.msg_reply);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + "返回");
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_reply;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        replyList.setNestedScrollingEnabled(false);// 解决滑动冲突
        msg_num_iv.setTypeface(iconfont);
        praise_iv.setTypeface(iconfont);
        getId = getIntent().getStringExtra("getId");
        pid = getIntent().getStringExtra("pid");
        listTip=(List<TipCommentBean>)getIntent().getSerializableExtra("getComment");
        //解决NestedScrollView嵌套RecyclerView滑动冲突导致无法正常调用加载更多功能
        nested_reply.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //判断是否滑到的底部
                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    //调用刷新控件对应的加载更多方法
                    // 最后一项完全显示, 触发操作, 执行加载更多操作 禁用回弹判断
                    if(replyContentList.size() % Constants.PAGE_SIZE==0){
                        getmAdapterWrapper().setLoadItemState(true);
                        onLoad();
                    }else {
                        getmAdapterWrapper().setLoadItemState(false);
                        getmLoadMoreHelper().setLoadMoreFinish();
                    }
                }
            }
        });
        getPresenter().setReplyContent(this,getId, pid, "1", page);
    }

    @NonNull
    @Override
    public ReplyPresenter createPresenter() {
        return new ReplyPresenter();
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return replyList;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_reply;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_message_empty, 0, 0);
        empty.setText(getResources().getString(R.string.null_msg));
    }

    @Override
    protected void convert(BaseViewHolder helper, ReplyContent item) {

        RoundedImageView iv_head = helper.itemView.findViewById(R.id.user_image);
        GlideApp.with(this).asBitmap().load(item.getUserImg()).placeholder(R.mipmap.img_default_head).into(iv_head);
        helper.setText(R.id.comment_user, item.getUserName());
        helper.setText(R.id.comment_content, item.getContent());
        TextView praise_iv = helper.itemView.findViewById(R.id.praise_iv);
        TextView msg_num_iv = helper.itemView.findViewById(R.id.msg_num_iv);
        TextView praise_num = helper.itemView.findViewById(R.id.praise_num);
        //点赞图标
        praise_iv.setTypeface(iconfont);
        msg_num_iv.setTypeface(iconfont);
        praise_num.setText(item.getLike() + "");
        //判断是否点赞
        if (item.isLike()) {// true 已点赞 false 未点赞
            praise_iv.setText(getResources().getString(R.string.comment_praise));
            praise_iv.setTextColor(getResources().getColor(R.color.main_color));
        } else {
            praise_iv.setText(getResources().getString(R.string.comment_praise_un));
            praise_iv.setTextColor(getResources().getColor(R.color.text_gray_99));
        }
        //时间
        helper.setText(R.id.comment_time, Datas.getTimeElapse(item.getTime()));
        praise_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isLike()) {
                    isLike = false;
                } else {
                    isLike = true;
                }
                helper.itemView.setTag(item.getId());
                getPresenter().addLikeClick(isLike, "" + item.getId());
                if (helper.itemView.getTag() != null) {
                    if (isLike && helper.itemView.getTag().equals(item.getId())) {
                        praise_num.setText("" + ((Integer.parseInt(praise_num.getText().toString())) + 1));
                        item.setLike(true);
                        praise_iv.setText(getResources().getString(R.string.comment_praise));
                        praise_iv.setTextColor(getResources().getColor(R.color.main_color));
                    } else {
                        item.setLike(false);
                        praise_num.setText("" + ((Integer.parseInt(praise_num.getText().toString())) - 1));
                        praise_iv.setText(getResources().getString(R.string.comment_praise_un));
                        praise_iv.setTextColor(getResources().getColor(R.color.text_gray_99));
                    }
                }
            }
        });
        helper.setOnClickListener(R.id.comment_tip, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listTip != null) {
                    getPresenter().tipComment(ReplyActivity.this, iconfont, listTip, "" + item.getId());
                }
            }
        });
        helper.setOnClickListener(R.id.comment_content, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().showCommentDialog(ReplyActivity.this, item, content);
            }
        });
    }

    @Override
    protected void onItemClick(View view, ReplyContent item) {

    }


    @Override
    public void onLoad() {
        super.onLoad();
        page++;
        getPresenter().setReplyContent(this,getId, pid, "1", page);
//        if(replyContentList.size()%Constants.PAGE_SIZE==0){
//
//        }else{
//            getmAdapterWrapper().setLoadItemVisibility(false);
//            getmLoadMoreHelper().setLoadMoreFinish();
//        }

    }

    @Override
    public void setData(List<ReplyContent> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        replyContentList.clear();
        replyContentList.addAll(data);
        if (data.size() % Constants.PAGE_SIZE != 0) {//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        }else{
            getmAdapterWrapper().setLoadItemVisibility(true);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEvent(Object result) {
        if (result instanceof ReplyJsonBean) {
            if (((ReplyJsonBean) result).returnCode.equals("0000")) {
                content = ((ReplyJsonBean) result).getReturnData().getMainComment();
                GlideApp.with(this).asBitmap().load(content.getUserImg()).placeholder(R.mipmap.img_default_head).into(user_image);
                comment_user.setText(content.getUserName());
                comment_content.setText(content.getContent());
                praise_num.setText(content.getLike() + "");
                if (content.isLike()) {// true 已点赞 false 未点赞
                    praise_iv.setText(getResources().getString(R.string.comment_praise));
                    praise_iv.setTextColor(getResources().getColor(R.color.main_color));
                } else {
                    praise_iv.setText(getResources().getString(R.string.comment_praise_un));
                    praise_iv.setTextColor(getResources().getColor(R.color.text_gray_99));
                }
                comment_time.setText(Datas.getTimeElapse(content.getTime()));
                tv_main.setText(getResources().getString(R.string.msg_reply) + "@" + content.getUserName());
                textview_title.setText(""+replyContentList.size() + getResources().getString(R.string.tiao) + getResources().getString(R.string.msg_reply));
                getAdapter().setLCEView(3);
                getAdapter().notifyDataSetChanged();
            }
        } else if (result instanceof BaseJSON) {
            if (((BaseJSON) result).returnMessage == null) {
                return;
            }
            if (((BaseJSON) result).returnMessage.equals("发布成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.input_sure_msg));
            } else if (((BaseJSON) result).returnMessage.equals("删除成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.delete_success));
            } else if (((BaseJSON) result).returnMessage.equals("举报成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.tip_success));
            } else if (((BaseJSON) result).returnMessage.equals("点赞成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.like_success));
            } else if (((BaseJSON) result).returnMessage.equals("取消点赞")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.like_cancel));
            }
        }
    }

    @OnClick(R.id.praise_iv)
    void dolike() {
        if (content.isLike()) {
            content.setLike(false);
        } else {
            content.setLike(true);
        }
        getPresenter().addLikeClick(content.isLike(), content.getId() + "");
        if (content.isLike()) {
            praise_num.setText("" + ((Integer.parseInt(praise_num.getText().toString())) + 1));
            content.setLike(true);
            praise_iv.setText(getResources().getString(R.string.comment_praise));
            praise_iv.setTextColor(getResources().getColor(R.color.main_color));
        } else {
            content.setLike(false);
            praise_num.setText("" + ((Integer.parseInt(praise_num.getText().toString())) - 1));
            praise_iv.setText(getResources().getString(R.string.comment_praise_un));
            praise_iv.setTextColor(getResources().getColor(R.color.text_gray_99));
        }
    }

    @OnClick(R.id.comment_tip_lay)
    void tip() {
        if (listTip != null) {
            getPresenter().tipComment(ReplyActivity.this, iconfont, listTip, "" + content.getId());
        }
    }

    @OnClick({R.id.tv_main, R.id.comment_content})
    void tocomment() {
        getPresenter().showCommentDialog(ReplyActivity.this, null, content);
    }
}
